

package top.continew.admin.question.manger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.continew.admin.question.manger.base.PageFactory;
import top.continew.admin.question.mapper.UserQuestionInfoMapper;
import top.continew.admin.question.mapper.question.QuestionMapper;
import top.continew.admin.question.mapper.test.TestMapper;
import top.continew.admin.question.model.*;
import top.continew.admin.question.manger.service.StatisticalService;
import top.continew.admin.question.manger.service.model.UserQuestionInfoList;

import java.util.List;

@Service
public class StatisticalServiceImpl implements StatisticalService {
    @Autowired
    private TestMapper testMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserQuestionInfoMapper userQuestionInfoMapper;

    @Override
    public void setQuestion() {
        List<QuestionSetInfo> testQuestionInfo = getTestQuestion();
        List<QuestionSetInfo> sectionQuestionInfo = getSectionQuestion();
        testQuestionInfo.addAll(sectionQuestionInfo);
        saveQuestion(testQuestionInfo);
    }

    @Override
    public List<QuestionInfoList> getQuestionInfoList(QuestionInfoQuery questionInfoQuery) {
        return PageFactory.page(() -> userQuestionInfoMapper.getQuestionInfoList(questionInfoQuery));
    }

    private List<QuestionSetInfo> getTestQuestion() {
        return testMapper.queryUseTestInfo();
    }

    private List<QuestionSetInfo> getSectionQuestion() {
        //获取被做过的章节
        List<QuestionSetInfo> sectionQuestionInfo = questionMapper.queryUseSection();
        for (QuestionSetInfo questionSetInfo : sectionQuestionInfo) {
            //查看题目数量
            String sum = String.valueOf(questionMapper.queryQuestionSetSum(questionSetInfo.getTypeId()));
            questionSetInfo.setSum(sum);
        }
        return sectionQuestionInfo;
    }

    private void saveQuestion(List<QuestionSetInfo> testQuestionInfo) {
        for (QuestionSetInfo questionSetInfo : testQuestionInfo) {
            QuestionSetInfo old = questionSetInfo.selectOne(new QueryWrapper<QuestionSetInfo>()
                .eq("code", questionSetInfo.getType() + "-" + questionSetInfo.getTypeId()));
            //存在更新
            if (old != null) {
                questionSetInfo.setId(old.getId());
            }
            questionSetInfo.setCode(questionSetInfo.getType() + "-" + questionSetInfo.getTypeId());
            questionSetInfo.insertOrUpdate();
        }
    }

    @Override
    public List<QuestionInfoUseInfo> getQuestionInfoUseInfo(String phone, String questionInfoId) {
        return PageFactory.page(() -> userQuestionInfoMapper.getUserByQuestionSet(phone, questionInfoId));
    }

    @Override
    public List<UserQuestionInfoList> getUserQuestionInfoList(UserQuestionInfoQuery userQuestionInfoQuery) {
        return PageFactory.page(() -> userQuestionInfoMapper.getUserQuestionInfoList(userQuestionInfoQuery));
    }

    @Override
    public List<UserQuestionSetList> getQuestionSetByUser(String name, String userId) {
        return PageFactory.page(() -> userQuestionInfoMapper.getQuestionSetByUser(name, userId));
    }
}
